package com.ujiuye.service.impl;

import com.ujiuye.dao.CourseDao;
import com.ujiuye.dao.impl.CourseDaoImpl;
import com.ujiuye.entity.Course;
import com.ujiuye.entity.Coursedetail;
import com.ujiuye.service.CourseService;
import com.ujiuye.util.PageBean;

import java.util.List;
import java.util.Map;

/**
 * CompanyName: IT优就业<br/>
 * Datetime: 2021/7/2 10:33<br/>
 *
 * @author TanJian
 */
public class CourseServiceImpl implements CourseService {
    CourseDao cd = new CourseDaoImpl();
    @Override
    public int addCourse(Course course) {
        return cd.addCourse(course);
    }


    /**
     * 封装PageBean
     * @param currentPage1
     * @param pageSize1
     * @param courseName
     * @return
     */
    @Override
    public PageBean getPageBean(String currentPage1, String pageSize1, String courseName) {



        //判断currentPage是否为空
        if (currentPage1 == null || currentPage1.trim().equals("")) {
            currentPage1 = "1";
        }
        if (pageSize1 == null || pageSize1.trim().equals("")) {
            pageSize1 = "3";
        }

        //1:当前页
        int currentPage = Integer.parseInt(currentPage1);
        //2:每页页容量
        int pageSize = Integer.parseInt(pageSize1);
        //3:计算总条数
        int totalCount = cd.totalCount(courseName);
        //4:计算总页数
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :totalCount / pageSize +1;
        //5:获取课程数据
        //获取起始下标
        int startIndex = (currentPage-1) * pageSize;

        List<Course> list =  cd.selectAllCourse(currentPage,startIndex,pageSize,courseName);

        //创建pageBean对象
        PageBean pb = new PageBean(currentPage,pageSize,totalCount,totalPage,list);

        return pb;
    }

    @Override
    public PageBean<Course> findAllSelect(String currentPage1, String pageSize1, String courseName, String courseType1) {
        if(currentPage1 == null || "".equals(currentPage1)){
            currentPage1 = "1";
        }

        if(pageSize1 == null || "".equals(pageSize1)){
            pageSize1 = "6";
        }

        if(courseType1 == null || "".equals(courseType1)){
            courseType1 = "0";
        }

        int currentPage = Integer.parseInt(currentPage1);
        int pageSize = Integer.parseInt(pageSize1);
        int courseType = Integer.parseInt(courseType1);

        int totalCount = cd.beforeTotalCount(courseName,courseType);
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :totalCount / pageSize +1;

        //获取起始下标
        int startIndex = (currentPage-1) * pageSize;
        List<Course> list = cd.beforeSelectAllCourse(courseName,courseType,startIndex,pageSize);

        PageBean<Course> pb = new PageBean<>(currentPage,pageSize,totalCount,totalPage,list);

        return pb;
    }

    @Override
    public Course findCourse(int cid) {
        return cd.findCourse(cid);
    }

    @Override
    public Map<String, List<Coursedetail>> findCourseDetail(int cid) {
        return cd.findCourseDetail(cid);
    }

    @Override
    public List<Course> findCourseByUid(int uid) {
        return cd.findCourseByUid(uid);
    }


    @Override
    public int delAll(String cids) {
        return cd.delAll(cids);
    }

    @Override
    public int updateCourse(Course course) {
        return cd.updateCourse(course);
    }

    @Override
    public int addCourseDetail(Coursedetail cd1) {
        return cd.addCourseDetail(cd1);
    }

    @Override
    public List<Course> findCourseByType(int courseType) {
        return cd.findCourseByType(courseType);
    }
}

